<?php

namespace App\Admin\Controllers;

use App\Models\Attendance_cycle;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;

class Attendance_cycleController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.attendance_cycle'));
            $content->description(trans('admin.list'));
            
            $content->body($this->grid()->render());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header(trans('admin.attendance_cycle'));
            $content->description(trans('admin.edit'));

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header(trans('admin.attendance_cycle'));
            $content->description(trans('admin.create'));

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(Attendance_cycle::class, function (Grid $grid) {
            $grid->id(trans('admin.ID'))->sortable();
            $grid->year(trans('admin.attendance_cycle_year'))->display(function () {
                    return $this->year.'学年';
            })->sortable();
            
            $grid->season(trans('admin.attendance_cycle_season'))->display(function () {
                if ($this->season==1) {
                    return trans('admin.spring').'学期';
                }elseif ($this->season==2) {
                    return trans('admin.autumn').'学期';
                }
            })->sortable();
            $grid->week(trans('admin.attendance_cycle_week'))->display(function () {
                    return '第'.$this->week.'周';
            })->sortable();
            $grid->start_date(trans('admin.attendance_cycle_start_date'))->sortable();
            $grid->end_date(trans('admin.attendance_cycle_end_date'))->sortable();
            $grid->status(trans('admin.status'))->editable('select', [0 => '启用', 1 => '禁用'])->sortable(); 

            //禁用导出按钮
            $grid->disableExport();
            
            // $grid->tools(function ($tools) { 
            //     //关闭批量删除 
            //     $tools->batch(function ($batch) { 
            //         $batch->disableDelete(); 
            //     });
            // });

            $grid->actions(function ($actions) {
                $actions->disableView();
            });
            $grid->filter(function ($filter) {
                $filter->column(1/2, function ($filter) {
                    $filter->disableIdFilter();
                    $filter->equal('year',trans('admin.attendance_cycle_year'))->year();
                    $filter->equal('week',trans('admin.attendance_cycle_week'))->select([
                            '1'   => '第1周',
                            '2'  => '第2周',
                            '3'  => '第3周',
                            '4'   => '第4周',
                            '5'  => '第5周',
                            '6'  => '第6周',
                            '7'   => '第7周',
                            '8'  => '第8周',
                            '9'  => '第9周',
                            '10'   => '第10周',
                            '11'  => '第11周',
                            '12'  => '第12周',
                            '13'   => '第13周',
                            '14'  => '第14周',
                            '15'  => '第15周',
                            '16'   => '第16周',
                            '17'  => '第17周',
                            '18'  => '第18周',
                            '19'   => '第19周',
                            '20'  => '第20周',
                            '21'   => '第21周',
                            '22'  => '第22周',
                            '23'  => '第23周',
                            '24'   => '第24周',
                            '25'  => '第25周',
                    ]);
                });
                $filter->column(1/2, function ($filter) {
                    $filter->equal('season', trans('admin.attendance_cycle_season'))->select([
                        ''   => trans('admin.all'),
                        '1'  => trans('admin.spring'),
                        '2'  => trans('admin.autumn'),
                    ]);
                    $filter->equal('status', trans('admin.status'))->select([
                        '0'   => trans('admin.enable'),
                        '1'  => trans('admin.disable')
                    ]);
                });              
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(Attendance_cycle::class, function (Form $form) {
            $form->display('id', 'ID');
            $form->date('year', trans('admin.attendance_cycle_year'))->format('YYYY')->rules('required')->addElementLabelClass('must-input');
            $form->select('season', trans('admin.attendance_cycle_season'))->options([1 => trans('admin.spring'), 2 => trans('admin.autumn')])->rules('required')->addElementLabelClass('must-input');
            // 设置最大值
            $form->number('week', trans('admin.attendance_cycle_week'))->max(25)->min(1)->rules('required')->addElementLabelClass('must-input');
            
            $form->date('start_date', trans('admin.attendance_cycle_start_date'))->format('YYYY-MM-DD')->rules('required')->addElementLabelClass('must-input');
            $form->date('end_date', trans('admin.attendance_cycle_end_date'))->format('YYYY-MM-DD')->rules('required')->addElementLabelClass('must-input');
            $form->radio('status', trans('admin.status'))->options([0 => trans('admin.enable'), 1 => trans('admin.disable')])->default(0);

            //保存后回调
            $form->saved(function (Form $form) {
                if ($form->season==1) {
                    $form->season = '春季学期';
                }elseif($form->season==2){
                    $form->season = '春季学期';
                }
                $name = $form->year.'学年'.$form->season.'第'.$form->week.'周';
                $max_id = Attendance_cycle::max('id');
                $squad = Attendance_cycle::find($max_id);
                $squad->name = $name;
                $squad->save();
            });
            
            //去掉添加/编辑表单的重置按钮
            $form->disableReset();
            $form->tools(function (Form\Tools $tools) {
                // 去掉`列表`按钮
                $tools->disableList();
                // 去掉`删除`按钮
                $tools->disableDelete();
                // 去掉`查看`按钮
                $tools->disableView();
            });
        });
    }
}
